package com.enjoy.okhttp;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;

import miguokhttp3.Call;
import miguokhttp3.Interceptor;
import miguokhttp3.OkHttpClient;
import miguokhttp3.Request;
import miguokhttp3.Response;
import miguokhttp3.ResponseBody;

public class MainActivity extends AppCompatActivity {
    OkHttpClient httpClient =
            new OkHttpClient.Builder().proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
                            "114.239.145.90", 808)))
                    .addInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            //.....
                            Response proceed = chain.proceed(chain.request());
                            //......
                            return proceed;
                        }
                    })
                    .build();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        try {
            URLConnection urlConnection = new URL("http://www.baidu.com").openConnection();
            System.out.println(urlConnection.getClass().getName());
        } catch (IOException e) {
            e.printStackTrace();
        }

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();

                Request request = new Request.Builder()
                        .url("http://restapi.amap.com/v3/weather/weatherInfo?city=长沙&key" +
                                "=13cb58f5884f9749287abbead9c658f2")
                        .build();
                //执行同步请求
                Call call = httpClient.newCall(request);
                Response response = null;
                try {
                    response = call.execute();
                    //获得响应
                    ResponseBody body = response.body();
                    System.out.println("http代理 响应==》" + body.string());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }


            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
